﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using APMS.WebSite.Common;
using APMS.WebSite.Models.DataStore;
using APMS.WebSite.Models.ViewData.Pages;

namespace APMS.WebSite.Controllers
{
    /// <summary>
    /// ChatController controls all Views related to Chat operation which are:
    /// * Index (/InProject/{projectId}/Chat/)
    /// * List (/InProject/{projectId}/Chat/List)
    /// * Create Room (/InProject/{projectId}/Chat/CreateRoom)
    /// * View Room (/InProject/{projectId}/Chat/ViewRoom/{id})
    /// * Delete Room (/InProject/{projectId}/Chat/Delete/{id})
    /// * Add Message (/InProject/{projectId}/Chat/AddMessage/{id})
    /// * MORE
    /// </summary>
    /// <version>$Revision$ $Date$</version>
    /// <history>
    /// <record>09/15/2008. raQuiam - Created this file</record>
    /// </history>
    [HandleError]
    public class ChatController : ProjectBaseController
    {
        #region Keys for ViewData
        /// <summary>
        /// Use in method List
        /// </summary>
        public const string ALL_CHAT_ROOMS = "AllChatRooms";
        #endregion

        #region List Action
        /// <summary>
        /// List all Todo under target project 
        /// </summary>
        /// <returns></returns>
        public ActionResult List(int projectId)
        {
            // Initialize data required by master page
            ActionResult exception = base.InitializeAction(MainNavItemType.Chat);
            if (exception != null)
                return exception;

            // Get current project
            Project currentProject = base.GetProject(projectId);

            // Set Window Title
            base.SetTitle(currentProject.ProjectName + " | Chat Rooms", true);
            base.SetInfoToUserControls(currentProject);

            // TODO: Query
            ViewData[CURRENT_PROJECT] = currentProject;

            return View("List");
        }
        #endregion

        #region Create Action
        /// <summary>
        /// Create new Message
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateRoom(int projectId)
        {
            return View("CreateRoom");
        }
        #endregion

        #region View/Update Actions
        public ActionResult ViewRoom(int projectId, int id)
        {
            return View("ViewRoom");
        }
        #endregion

        #region Delete Action
        public ActionResult DeleteRoom(int projectId, int id)
        {
            // Redirect to  List Todos
            return RedirectToAction("List", new { projectId = projectId });
        }
        #endregion

        #region Add Message Action
        /// <summary>
        /// Callback action
        /// </summary>
        public void AddMessage()
        {
        }
        #endregion

        #region From Oat and old implementation
        ////#region Member Variables
        ////// TODO: Read connection string from DB, has DBFacade to handle reference object
        ////ApMs database = new ApMs(new OracleConnection("Data Source=raquiam-c82b211;Persist Security Info=True;User ID=apms;Password=apms;Unicode=True"));
        ////#endregion
        
        //public ActionResult Index()
        //{
        //    // TODO: Should we support list all Chat Rooms in every projects? /Chat/
        //    throw new NotImplementedException();
        //}
        ////"Project",      // Route name

        //public ActionResult create(int projectId,String chatroom_name)
        //{
        //    //throw new NotImplementedException();   
        //    String username = "admin";
        //    MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();        
        //    Chat newChatRoom = new Chat();
        //    newChatRoom.ProjectID = projectId;
        //    newChatRoom.ChatRoomName = chatroom_name;
        //    newChatRoom.UserName = username;
        //    newChatRoom.AddDate = DateTime.Now;


        //    db.Chat.Add(newChatRoom);
        //    db.SubmitChanges();
        //    return View("List");
        //}

        //public ActionResult Delete(int projectId , int id )
        //{
        //    //throw new NotImplementedException();
        //    MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();


        //    try
        //    {
        //        Chat deleteChatroom = db.Chat.Single(p => p.ChatID == id);
        //        db.Chat.Remove(deleteChatroom);
        //        db.SubmitChanges();
        //    }
        //    catch (InvalidOperationException ex)
        //    {
        //        System.Console.WriteLine("called on set with zero items");
        //    }
        //    ////////////////////// DELETE DATA

        //    return Index();

        //    return View("List");
        //}



        //public ActionResult Update(int projectId , int id)
        //{
        //    // Initialize data required by master page
            
        //    ActionResult exception = base.InitializeAction();
        //    if (exception != null)
        //        return exception;

        //    // Ser Window Title
        //    base.SetTitle("Chatroom | " + id);

        //    // SELECT * FROM user WHERE user.UserName = '{currentUser}'
        //    MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
        //    var query =
        //        from chat in db.Chat
        //        where
        //            chat.ChatID == id
        //        select chat;

        //    // Prepare ViewData
        //    ManageChatViewData viewData = new ManageChatViewData();
        //    viewData.CurrentChat = query.First<Chat>();

        //    //TODO: Handle both POST and GET methods
        //    if (Request.HttpMethod == Constants.HttpMethod.POST)
        //    {
        //        // If POST means UPDATE the value from Request.Form to DB
        //        // If GET means VIEW the value from DB
        //        #region Update record back to DB
        //        viewData.CurrentChat.ChatRoomName = Request.Form["chatroomname"];

        //        viewData.IsCurrentChatUpdate = true;

        //        db.SubmitChanges();
        //        #endregion
        //    }
        //    else
        //    {
        //        viewData.IsCurrentChatUpdate = false;
        //    }

        //    return View("Update", viewData);
        //}

        //public ActionResult List(int projectId)
        //{
        //    MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
        //    var query =
        //        from chat in db.Chat
        //        where
        //            chat.ProjectID == projectId
        //        select chat;


        //    ViewData["all"] = query.ToList();
        //    ViewData["justwanttopreviewmassage"] = "List ";
        //    return View("List");
        //}
        #endregion
    }
}
